(in-package :gwee)

(defclass title-bar-mixin ()
  ((bar-title :initarg :title :accessor title-bar-title)
   (bar-size :initform 20 :initarg :title-size :accessor title-bar-size)
   (bar-background :initarg :title-background :initform (color 0 0 200) :accessor title-bar-background)
   (bar-foreground :initarg :title-foreground :initform (gray 255) :accessor title-bar-foreground)
   (bar-font :initarg :title-font :initform (default-font) :accessor title-bar-font)
   (bar-side :initarg :title-side :initform :top :accessor title-bar-side))
  (:documentation "Add a title bar along one edge to a view."))

(defun title-bar-rectangle (view &optional top-coordinate-space)
  (with-slots ((side bar-side) (size bar-size)) view
    (values				; x,y,w,h
     (ecase side
       ((:top :bottom :left) (if top-coordinate-space (view-cache-top-x view) 0))
       (:right  (- (view-width view) size)))
     (ecase side
       ((:top :left :right) (if top-coordinate-space (view-cache-top-y view) 0))
       (:bottom (- (view-height view) size)))
     (ecase side
       ((:top :bottom) (view-width view))
       ((:left :right) size))
     (ecase side
       ((:top :bottom) size)
       ((:left :right) (view-height view))))))

(defmethod draw-view :after ((view title-bar-mixin))
  ;; FIXME: should we just use a text-area view?
  (with-slots ((title bar-title) (side bar-size) (size bar-size) (bg bar-background) (fg bar-foreground) (font bar-font)) view
    (let ((fheight (sdl:get-font-height :font font))
	  (fwidth (sdl:get-font-size title :size :w :font font)))
      (multiple-value-bind (x y w h) (title-bar-rectangle view t)
	(sdl:draw-box-* x y w h :color bg)
	;; FIXME: how do we draw vertical strings?
	(unless (find side '(:left :right))
	  (sdl:draw-string-blended-* title
				     (+ x (truncate (- (view-width view) fwidth) 2))
				     (+ y (truncate (- size fheight) 2))
				     :color fg :font font))))))
